""" 
@coding  : utf-8
@Time    : 2023/5/31 20:44
@Author  : Rumou
@File    : 爬取图片.py
@Description:
"""
import re

import requests

# 获取网站数据
url = requests.get('https://www.sanchuangedu.cn/')
url.encoding = 'utf-8'
html = url.text
# 正则表达式
img = re.finditer(r"(<img.*src.*=)(.*)(width.*)", html, re.M)
image_urls = []
for i in img:
    img_file = ("https://www.sanchuangedu.cn/" + i.group(2)).strip()
    image_urls.append(img_file)

num = 1
# 遍历下载
for i in image_urls:
    print("正在下载图片:", i)
    if i.endswith("jpg"):
        get = requests.get(i)
        f = open(f"{num}.jpg", 'wb')
        f.write(get.content)
        f.close()
    elif i.endswith("png"):
        get = requests.get(i)
        f = open(f"{num}.png", 'wb')
        f.write(get.content)
        f.close()
    print(f"第{num}张图片下载完毕")
    num = num + 1
